home *** CD-ROM | disk | FTP | other *** search
/ MacWorld 1998 October / Macworld (1998-10).dmg / Shareware World / Info / For Developers / MacZoop 1.8.4 / More Classes / Streaming Classes / ZFileStream.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-07-16  |  2.8 KB  |  123 lines  |  [TEXT/CWIE]

  1. /*************************************************************************************************
  2. *
  3. *
  4. *            MacZoop - "the framework for the rest of us"         
  5. *
  6. *
  7. *
  8. *            ZFileStream.cpp        -- a stream using file storage
  9. *
  10. *
  11. *
  12. *
  13. *
  14. *            © 1998, Graham Cox
  15. *
  16. *
  17. *
  18. *
  19. *************************************************************************************************/
  20.  
  21.  
  22. #include    "ZFileStream.h"
  23. #include    "ZErrors.h"
  24.  
  25.  
  26. /*--------------------------------***  CONSTRUCTOR  ***---------------------------------*/
  27.  
  28. ZFileStream::ZFileStream( const FSSpec& aFileSpec )
  29.     : ZFile( aFileSpec ), ZStream()
  30. {
  31.     classID = CLASS_ZFileStream;
  32.     
  33.     InitFileStream();
  34. }
  35.  
  36. /*--------------------------------***  CONSTRUCTOR  ***---------------------------------*/
  37.  
  38. ZFileStream::ZFileStream( Str255 fName )
  39.     : ZFile( fName ), ZStream()
  40. {
  41.     classID = CLASS_ZFileStream;
  42.     
  43.     InitFileStream();
  44. }
  45.  
  46. /*--------------------------------***  CONSTRUCTOR  ***---------------------------------*/
  47.  
  48. ZFileStream::ZFileStream()
  49.     : ZFile(), ZStream()
  50. {
  51.     classID = CLASS_ZFileStream;
  52. }
  53.  
  54.  
  55. /*---------------------------------***  DESTRUCTOR  ***---------------------------------*/
  56.  
  57. ZFileStream::~ZFileStream()
  58. {
  59.     if ( dataWasWritten )
  60.         SetLength( GetMark());
  61. }
  62.  
  63.  
  64. /*------------------------------------***  RESET  ***-----------------------------------*/
  65. /*
  66. reset the stream in order to re-read it from the beginning (use VERY carefully!)    
  67. ----------------------------------------------------------------------------------------*/
  68.  
  69. void    ZFileStream::Reset()
  70. {
  71.     SetMark( 0 );
  72. }
  73.  
  74.  
  75. /*------------------------------------***  SKIP  ***------------------------------------*/
  76. /*    
  77. move stream mark without reading or writing data
  78. ----------------------------------------------------------------------------------------*/
  79.  
  80. void    ZFileStream::Skip( long bytesToSkip )
  81. {
  82.     FailOSErr( SetFPos( refNum, fsFromMark, bytesToSkip ));
  83. }
  84.  
  85.  
  86. /*-----------------------------------***  PUTTO  ***------------------------------------*/
  87. /*    
  88. write data to stream storage (file)
  89. ----------------------------------------------------------------------------------------*/
  90.  
  91. void    ZFileStream::PutTo( void* data, long dataLen )
  92. {
  93.     Write((Ptr) data, &dataLen );
  94.     
  95.     dataWasWritten = TRUE;
  96. }
  97.  
  98.  
  99. /*-----------------------------------***  GETFROM  ***----------------------------------*/
  100. /*    
  101. read data from stream storage (file)
  102. ----------------------------------------------------------------------------------------*/
  103.  
  104. void    ZFileStream::GetFrom( void* data, long* dataLen )
  105. {
  106.     Read((Ptr) data, dataLen );
  107. }
  108.  
  109. /*-------------------------------***  INITFILESTREAM  ***-------------------------------*/
  110. /*
  111. common initialiser opens the file ready to read or write.    
  112. ----------------------------------------------------------------------------------------*/
  113.  
  114. void    ZFileStream::InitFileStream()    
  115. {
  116.     if ( ! IsReal())
  117.         Create();
  118.     
  119.     Open();
  120.     
  121.     dataWasWritten = FALSE;
  122. }    
  123.